package com.winning.sx.microframework.service;

import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.dao.EasyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Map;

@Service
public class EasyService {
    @Autowired
    private EasyDao easyDao;

    public DataTable queryList(String TABLE_NAMNE, Map<String, String> param) throws SQLException {
        return easyDao.queryList(TABLE_NAMNE, param);
    }

    public Map queryEntity(String TABLE_NAMNE, Map<String, String> param) throws SQLException {
        return easyDao.queryEntity(TABLE_NAMNE, param);
    }

    public boolean isExit(String TABLE_NAMNE, String idColumn, Map<String, String> param) throws SQLException {
        return easyDao.isExit(TABLE_NAMNE, idColumn, param);
    }

    public boolean updatestate(String TABLE_NAMNE, String idColumn, Map<String, String> param) throws SQLException {
        if (param.get(idColumn) != null && !"".equals(param.get(idColumn))) {
            return easyDao.updatestate(TABLE_NAMNE, idColumn, param);
        } else {
            return easyDao.insert(TABLE_NAMNE, param);
        }
    }

    public boolean backone(String TABLE_NAMNE, String idColumn, Map<String, String> param) throws SQLException {
        if (param.get(idColumn) != null && !"".equals(param.get(idColumn))) {
            return easyDao.backone(TABLE_NAMNE, idColumn, param);
        } else {
            return easyDao.insert(TABLE_NAMNE, param);
        }
    }

    public boolean save(String TABLE_NAMNE, String idColumn, Map<String, String> param) throws SQLException {
        if (param.get(idColumn) != null && !"".equals(param.get(idColumn))) {
            return easyDao.update(TABLE_NAMNE, idColumn, param);
        } else {
            return easyDao.insert(TABLE_NAMNE, param);
        }
    }

    public boolean insert(String TABLE_NAMNE, Map<String, String> param) throws SQLException {
        return easyDao.insert(TABLE_NAMNE, param);
    }

    public boolean update(String TABLE_NAMNE, String idColumn, Map<String, String> param) throws SQLException {
        return easyDao.update(TABLE_NAMNE, idColumn, param);
    }

    public boolean update(String TABLE_NAMNE, Map<String, String> queryParam, Map<String, String> param) throws SQLException {
        return easyDao.update(TABLE_NAMNE, queryParam, param);
    }

    public boolean delete(String TABLE_NAMNE, String idColumn, Map<String, String> param) throws SQLException {
        return easyDao.delete(TABLE_NAMNE, idColumn, param);
    }

    public boolean deleteAll(String TABLE_NAMNE) throws SQLException {
        return easyDao.deleteAll(TABLE_NAMNE);
    }

    public boolean deleteDeep(String sql, Map<String, String> param) throws SQLException {
        return easyDao.deleteDeep(sql, param);
    }

    public DataTable querySql(String tableName, String columns, Map<String, String> param, String where, String orderBy, String groupBy) throws SQLException {
        return easyDao.querySql(tableName, columns, param, where, orderBy, groupBy);
    }

    public DataTable querySql(String sqlStr, Map<String, String> param) throws SQLException {
        return easyDao.querySql(sqlStr, param);
    }

    public DataTable querySqlByParam(String sqlStr, Map<String, Object> param) throws SQLException {
        return easyDao.querySqlByParam(sqlStr, param);
    }

    public DataTable querySqlInPage(String tableName, String columns, Map<String, String> param, String where, String orderBy, String groupBy, int pageSize, int pageStart) throws SQLException {
        return easyDao.querySqlInPage(tableName, columns, param, where, orderBy, groupBy, pageSize, pageStart);
    }

    public void ChangeBeanToStringMap(Map<String, Object> param) {
        for (String key : param.keySet()) {
            System.out.print(key + ",");
            if (param.get(key) == null) {
                continue;
            }
            Class<? extends Object> valueClass = param.get(key).getClass();
            param.put(key, param.get(key).toString());
        }
        System.out.println("");
    }

    public void NotNull(Map param, String keys[], String names[]) throws Exception {
        for (int i = 0; i < keys.length; i++) {
            String key = keys[i];
            if (param.get(key) == null) throw new Exception(names[i] + "字段不能为空!");
            if ("".equals(param.get(key).toString().trim())) throw new Exception(names[i] + "字段不能为空!");
            if ("null".equals(param.get(key).toString().trim().toLowerCase()))
                throw new Exception(names[i] + "字段不能为空!");
        }
    }
}
